// import Vue from "vue";

import App from './App.vue'
import router from './router'

// import ElementUI from 'element-ui'
// import 'element-ui/lib/theme-chalk/index.css';
// CSS resets
import 'normalize.css/normalize.css'
// global css
import './styles/index.scss'
// 导入网站工具
import helper from './helper.js'
// 引用网站全局文件
import site from './site.js'
// 缓存
import store from './vuex/store.js'
// 剪切板功能
import VueClipboard from 'vue-clipboard2'
// 过滤器
import * as filters from './common/js/filters.js'
import Meta from 'vue-meta'

// 缓存
import localStore from './common/js/store'

/* eslint-disable no-undef */
Vue.use(VueClipboard)

// Vue.use(ElementUI)
Vue.use(helper)

Object.keys(filters).forEach((key) => {
  Vue.filter(key, filters[key])
})

Vue.prototype.site = site

Vue.use(Meta)
Vue.config.productionTip = false
// 路由拦截
router.beforeEach((to, from, next) => {
  store.dispatch('setLoading', 1)
  /* 页面title */
  if (to.meta.title) {
    document.title = to.meta.title
  }
  /* 判断该路由是否需要登录权限,不需要权限的或已经登录的直接执行下一步 */
  if (to.matched.some((record) => record.meta.needLogin === false) || store.getters['login/isLogin']) {
    // 权限验证
    if (!hasPermission(to.meta)) {
      // 跳转至没有权限
      next({
        path: '/index'
      })
    }
    next()
  } else {
    // 需要授权的,未登录的情况下,跳转到登录
    localStore.delete('topIndex');
    localStore.delete('sndTitle');
    localStore.delete('trdTitle');
    next({
      path: '/login'
    })
  }
})

function hasPermission(meta) {
  if (meta && typeof meta == 'string') {
    meta = {
      permission: meta
    }
  }
  const { permission: node } = {
    permission: false,
    ...meta
  }
  if (node === false) {
    return true
  }
  // 权限验证
  const auths = store.getters['navi/nodes']
  if (!auths.includes(node)) {
    return false
  }

  return true
}

Vue.prototype.hasPermission = hasPermission
// 定义权限节点指令
Vue.directive('auth', {
  inserted(el, bind, vnode) {
    if (!hasPermission(bind.value)) {
      el.parentNode.removeChild(el)
    }
  }
})

new Vue({
  router,
  store,
  render: (h) => h(App)
}).$mount('#app')
